/**
 * Project Name:HomeActivity
 * Date:2016-4-19上午10:55:35
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.view.EditText;

import java.lang.reflect.Method;

import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.keyboard.DefineKeyboardUtil;

import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * ClassName: KkdEditText <br/>
 * date: 2016-4-19 上午10:55:35 <br/>
 *
 * @author askzhao
 * @version 
 */
public class KkdEditText extends EditText {


	private Context mContext;
	
	public KkdEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}


	public KkdEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}


	public KkdEditText(Context context) {
		super(context);
		mContext = context;
		init();
	}
	
	private void init() {
		hideInputType();
	}

	
	/**
	 * 判断当前系统版本，选择使用何种方式隐藏默认键盘
	 */
	private void hideInputType() {
		int SDK_INT = android.os.Build.VERSION.SDK_INT;
		if (SDK_INT <= 10) {
			// 屏蔽默认输入法
			setInputType(InputType.TYPE_NULL);

		} else {
			((Activity) mContext).getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
			/** 方法一 */
			try {
				Class<EditText> cls = EditText.class;
				Method setShowSoftInputOnFocus;
				setShowSoftInputOnFocus = cls.getMethod(
						"setShowSoftInputOnFocus", boolean.class);
				setShowSoftInputOnFocus.setAccessible(true);
				setShowSoftInputOnFocus.invoke(this, false);
			} catch (Exception e) {
				e.printStackTrace();
			}

			/** 方法二 */
			InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(this.getWindowToken(), 0);

		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mContext instanceof BaseActivity) {
			DefineKeyboardUtil.getInstance().setEditText(KkdEditText.this);
			DefineKeyboardUtil.getInstance().showKeyboard();
		}

		return super.onTouchEvent(event);
	}
}
